package com.qian.saas.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qian.saas.domain.Dev;
import com.qian.saas.domain.Rep;
import com.qian.saas.domain.Users;
import com.qian.saas.domain.Vo.RepVo;
import com.qian.saas.mapper.DevMapper;
import com.qian.saas.mapper.RepMapper;
import com.qian.saas.mapper.UsersMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rep")
@CrossOrigin
public class RepController {

    @Resource
    private RepMapper repMapper;

    @Resource
    private DevMapper devMapper;

    @Resource
    private UsersMapper usersMapper;

    @GetMapping("/list")
    public R sellist(String page,String limit,String sort){
        System.out.println(page);
        IPage<Rep> mppage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));//参数一：当前页，参数二：每页记录数
        //分页
        IPage<Rep> userIPage = repMapper.selectPage(mppage, null);
        List<Rep> records = userIPage.getRecords();
        List repVos=new ArrayList();
        for (Rep record : records) {
            RepVo repVo=new RepVo();
            String tupian = record.getTupian();
            if(tupian.length()!=0){
                tupian=tupian.substring(0,tupian.length()-1);
                String[] split = tupian.split(",");
                for (int i=0;i<split.length;i++) {
                    split[i]="http://127.0.0.1/file/"+split[i];
                }
                repVo.setTupians(split);
            }
            if(record.getDevid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("devid",record.getDevid());
                Dev dev = devMapper.selectOne(wrapper);
                repVo.setDevname(dev.getDevname());
            }
            if (record.getUserid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("id",record.getUserid());
                Users users = usersMapper.selectOne(wrapper);
                repVo.setUsername(users.getUsername());
            }
            repVo.setDevid(record.getDevid());
            repVo.setRepid(record.getRepid());
            repVo.setCreatetime(record.getCreatetime());
            repVo.setUptime(record.getUptime());
            repVo.setQu(record.getQu());
            repVo.setJlqu(record.getJlqu());
            if (record.getSt()==1){
                repVo.setSts("等待维修");
            } else if (record.getSt()==2) {
                repVo.setSts("正在维修");
            }else {
                repVo.setSts("维修完成");
            }
            repVos.add(repVo);
        }
        Map map=new HashMap<>();
        map.put("items",repVos);
        map.put("total",userIPage.getTotal());
        return R.success(map);
    }

    @PostMapping("/repupdate")
    public R repupdate(@RequestBody Rep rep){
        System.out.println(rep);
        UpdateWrapper<Rep> wrapper=new UpdateWrapper<>();
        wrapper.eq("repid",rep.getRepid());
        wrapper.set("userid",rep.getUserid());
        wrapper.set("jlqu",rep.getJlqu());
        wrapper.set("uptime", LocalDateTime.now());
        wrapper.set("st", 2);
        repMapper.update(null,wrapper);
        return R.success(rep);
    }

    @GetMapping("/wxlist")
    public R selwxlist(String page,String limit,String name){
        System.out.println(name);
        System.out.println(page);
        IPage<Rep> mppage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));//参数一：当前页，参数二：每页记录数
        //分页
        QueryWrapper  userwrapper=new QueryWrapper();
        userwrapper.eq("username",name);
        Users users1 = usersMapper.selectOne(userwrapper);
        QueryWrapper  userlistwrapper=new QueryWrapper();
        userlistwrapper.eq("userid",users1.getId());
        IPage<Rep> userIPage = repMapper.selectPage(mppage, userlistwrapper);
        List<Rep> records = userIPage.getRecords();
        List repVos=new ArrayList();
        for (Rep record : records) {
            RepVo repVo=new RepVo();
            String tupian = record.getTupian();
            if(tupian.length()!=0){
                tupian=tupian.substring(0,tupian.length()-1);
                String[] split = tupian.split(",");
                for (int i=0;i<split.length;i++) {
                    split[i]="http://127.0.0.1/file/"+split[i];
                }
                repVo.setTupians(split);
            }
            if(record.getDevid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("devid",record.getDevid());
                Dev dev = devMapper.selectOne(wrapper);
                repVo.setDevname(dev.getDevname());
            }
            if (record.getUserid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("id",record.getUserid());
                Users users = usersMapper.selectOne(wrapper);
                repVo.setUsername(users.getUsername());
            }
            repVo.setDevid(record.getDevid());
            repVo.setRepid(record.getRepid());
            repVo.setCreatetime(record.getCreatetime());
            repVo.setUptime(record.getUptime());
            repVo.setQu(record.getQu());
            repVo.setJlqu(record.getJlqu());
            if (record.getSt()==1){
                repVo.setSts("等待维修");
            } else if (record.getSt()==2) {
                repVo.setSts("正在维修");
            }else {
                repVo.setSts("维修完成");
            }
            repVos.add(repVo);
        }
        Map map=new HashMap<>();
        map.put("items",repVos);
        map.put("total",userIPage.getTotal());
        return R.success(map);
    }

    @GetMapping("/wxreplist")
    public R wxreplist(String page,String limit,String sort){
        System.out.println(page);
        IPage<Rep> mppage = new Page<>(Integer.parseInt(page),Integer.parseInt(limit));//参数一：当前页，参数二：每页记录数
        //分页
        QueryWrapper repwrapper =new QueryWrapper();
        repwrapper.eq("st",3);
        IPage<Rep> userIPage = repMapper.selectPage(mppage, repwrapper);
        List<Rep> records = userIPage.getRecords();
        List repVos=new ArrayList();
        for (Rep record : records) {
            RepVo repVo=new RepVo();
            String tupian = record.getTupian();
            if(tupian.length()!=0){
                tupian=tupian.substring(0,tupian.length()-1);
                String[] split = tupian.split(",");
                for (int i=0;i<split.length;i++) {
                    split[i]="http://127.0.0.1/file/"+split[i];
                }
                repVo.setTupians(split);
            }
            if(record.getDevid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("devid",record.getDevid());
                Dev dev = devMapper.selectOne(wrapper);
                repVo.setDevname(dev.getDevname());
            }
            if (record.getUserid()!=null){
                QueryWrapper wrapper=new QueryWrapper();
                wrapper.eq("id",record.getUserid());
                Users users = usersMapper.selectOne(wrapper);
                repVo.setUsername(users.getUsername());
            }
            repVo.setDevid(record.getDevid());
            repVo.setRepid(record.getRepid());
            repVo.setCreatetime(record.getCreatetime());
            repVo.setUptime(record.getUptime());
            repVo.setQu(record.getQu());
            repVo.setJlqu(record.getJlqu());
            if (record.getSt()==1){
                repVo.setSts("等待维修");
            } else if (record.getSt()==2) {
                repVo.setSts("正在维修");
            }else {
                repVo.setSts("维修完成");
            }
            repVos.add(repVo);
        }
        Map map=new HashMap<>();
        map.put("items",repVos);
        map.put("total",userIPage.getTotal());
        return R.success(map);
    }

}
